$(function() {
layui.use(['form', 'util', 'layer', 'laypage'], function() {
	var form = layui.form;
	form.render();
	var util = layui.util;
	var laypage = layui.laypage;
	var layer = layui.layer;
	var total = 0;
	BaseUtil.ajaxSetup();
	var fileName1,filevalue1;
	//定义一个获取角色的函数，参数为(页面，函数)
	function find_role_list(page, rows,fileName1,filevalue1) {
		$.ajax({
			type: "get",
			url: "http://localhost:8080/ssmService/role/selectPageRole",
			data: {
				"queryParams.curr_page": page,
				"queryParams.page_size": rows,
				"baseModel.listAdvancedQuery[0].fieldName":fileName1,
				"baseModel.listAdvancedQuery[0].fieldValue":filevalue1

			},
			async: false,
			"success": function(result) {
				//result,有接口土工给我们的数据，即使接口返回值
				if(result.resultCode == 0) {
					total = result.data.total;
					build_role_List(result);
				} else {
					layer.msg("服务器错误，请联系管理员", {
						icon: 1,
						time: 1000
					});
				}
			},
			
		});
	}

	//渲染页面数据
	function build_role_List(result) {
		var roleList = result.data.list;
		$(".table_tbody").empty();
		$.each(roleList, function(index, item) {
			var tr = $("<tr></tr>");
			var td1 = $("<td></td>").append("<input type='checkbox' class='childrencheckBox' role_id='" + item.role_id + "' lay-skin='primary'>");
			var td2 = $("<td></td>").append(item.name);
			var td3 = $("<td></td>").append(item.memo);
			var td4 = $("<td></td>").append(item.update_user_name);
			var td5 = $("<td></td>")
				.append(util.toDateString(item.update_time, "yyyy-MM-ss HH:mm:ss"));
			var td6 = $("<td></td>").append("<button role_id='" + item.role_id + "' class='layui-btn layui-btn-sm updateRole'>编辑</button>")
				.append("<button role_id='" + item.role_id + "' class='layui-btn layui-btn-sm layui-btn-danger deleteRole'>删除</button>");
			tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).appendTo(".table_tbody");
		});
		form.render();
	}
	
	find_role_list(1, 10,null,null);
	$(".search").click(function(){
		filevalue1=$("#name").val();
		fileName1="name";
		find_role_list(1, 10,fileName1,filevalue1);
		
		laypage.render({
			elem: "table_page",
			count: total,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			jump: function(obj, first) {
				if(!first) {
					find_role_list(1, 10,fileName1,filevalue1);
				}
			}
	});
		
		});
		
		laypage.render({
			elem: "table_page",
			count: total,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			jump: function(obj, first) {
				if(!first) {
					find_role_list(1, 10,fileName1,filevalue1);
				}
			}
	});
	
	
		//删除角色
	$(document).on("click", ".deleteRole", function() {
		var role_id =$(this).attr("role_id");
		layer.confirm("您确定要删除吗?", function() {
			$.ajax({
				type: "delete",
				url: "http://localhost:8080/ssmService/role/deleteRole/" + role_id,
				async: true,
				"success": function(result) {
					layer.msg("删除成功", {
						icon: 1,
						time: 1000
					});
					location.reload();
				}
			});
		});
	});
		
		
		
		//批量删除
	$(".batchDelete").click(function() {
		var ids = "";
		var checkLength=0;
		$(".childrencheckBox").each(function() {
			if($(this).is(":checked")) {
				ids+= $(this).attr("role_id")+",";
				checkLength++;
			};
		});
		if (checkLength>0) {
		var roleId=ids.substring(0,ids.length-1);
		layer.confirm("您确定要删除"+checkLength+"条记录吗", function() {
			$.ajax({
				type: "delect",
				url: "http://localhost:8080/ssmService/role/deleteRole/" + roleId,
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg("删除成功", {
							icon: 1,
							time: 1000
						});
						location.reload();
					}
				}
			});
		})
		}else{
			layer.msg("至少需要选择一条记录才能删除",{
				icon:2,
				time:1000
			});
		}
	});
		
	form.on("checkbox(parentCheckBox)",function(data){
		if(data.elem.checked){
			$(".childrencheckBox").prop("checked",true);
		}else{
			$(".childrencheckBox").prop("checked",false);
		}
		form.render();
	});
	
	form.on("checkbox",function(data){
		var length=0;
		var checkLength = 0; 
		$(".childrencheckBox").each(function(){
			length++;
			if($(this).is(":checked")){
				checkLength++;
			}
		});
		if(checkLength==length){
			$(".parentCheckBox").prop("checked",true);
		}else{
			$(".parentCheckBox").prop("checked",false);
		}
		form.render();
	});
	



		
	$(".addUser").click(function() {
		var index = layer.open({
			type: 2,
			content: 'insertrole.html',
			title: '新增角色信息',
			maxmin: true,
			closeBtn:1,
		});
		layer.full(index);
	});

	$(document).on("click",".updateRole",function(){
		var roleId=$(this).attr("role_id");
			var index = layer.open({
			type: 2,
			content: 'EditorRole.html',
			title: false,
			maxmin: true,
			closeBtn: 1,
			content:'EditorRole.html?roleId='+roleId
		});
		layer.full(index);
	});

});
});